//
//  CityCell.m
//  Lovely Food
//
//  Created by 李经纬 on 15/12/8.
//  Copyright © 2015年 李经纬. All rights reserved.
//

#import "CityCell.h"
#import "CityCollectionCell.h"
#import "cityModel.h"
#import "UIImageView+WebCache.h"
#define KscreenHeight [[UIScreen mainScreen] bounds].size.height
#define KscreenWidth [[UIScreen mainScreen] bounds].size.width
@implementation CityCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupView];
        
    }
    return self;
}

-(NSMutableArray *)cityDataArray
{
    if (_cityDataArray == nil) {
        _cityDataArray = [[NSMutableArray alloc]init];
    }
    return _cityDataArray;
}

-(void)setupView
{
    
    
    
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((KscreenWidth/4), (KscreenWidth/3));
    layout.minimumLineSpacing = 20;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    //头高度 给标题留空间
    layout.headerReferenceSize = CGSizeMake(100, 60);
    //不写这句item不会显示！！！！！！！
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    self.collectionView.scrollEnabled = NO;
    
    [self.collectionView registerClass:[CityCollectionCell class] forCellWithReuseIdentifier:@"Item"];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collectionView];
    
    
    UILabel * title = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)];
    title.text = @"热门目的地";
    title.textColor = [UIColor blackColor];
    //title.backgroundColor = [UIColor blueColor];
    [self.collectionView addSubview:title];
    
    UILabel * sign = [[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(title.frame)+5, 200, 30)];
    sign.text = @"一座城市一种味道";
    sign.textColor = [UIColor grayColor];
    sign.font = [UIFont systemFontOfSize:14];
    //    sign.backgroundColor = [UIColor yellowColor];
    [self.collectionView addSubview:sign];
    
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
    [self getData];
    
    
}

//CityCell解析
-(void)getData
{
//    NSURLSession * session = [NSURLSession sharedSession];
//    
//    NSURL * url = [NSURL URLWithString:@"http://www.youyuwei.com/api/country?app_code=com.youyuwei.yuwei.ios&hardware_model=iPhone5%2C2&app_ver=2.1&name=%E4%B8%AD%E5%9B%BD&sys_ver=9.1&device_type=ios&ywsdk_ver=20131018&open_udid=086035be98bdf57eb36141a1a8b4c010171912b6&start=0&oauth_timestamp=1449406682&list=city&device_token=5fedbc3e6bc4a9aa4223bc304eaaf624a464b0cc3222ba15a80106126ed7cf45&oauth_signature_method=HMAC-SHA1&x_auth_mode=client_auth&ver=2&uid=18080&idfa=73BEDD47-7F7A-4DCC-8F35-15111C2264FE&channel_id=App%20Store&oauth_token=18080_ebbf4985ba91d7e9ed8fecf6105868c7&oauth_consumer_key=4&oauth_version=1.0&oauth_signature=Ml5REcw9IaU2GfM0H72SbaKJMbk%3D&debug=0&oauth_nonce=db219ed0118eab98d3fe5981a05aeabe"];
    
//    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];
//    NSURLSessionDataTask * task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
//        
//        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];
//        
//        
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            [self.collectionView reloadData];
//        });
//    }];
//    [task resume];
    
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 15;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.cityDataArray.count != 0) {
        CityCollectionCell * item = [collectionView dequeueReusableCellWithReuseIdentifier:@"Item" forIndexPath:indexPath];
        
        cityModel * model = self.cityDataArray[indexPath.row];
        item.cityName.text = model.name;
        [item.cityImage sd_setImageWithURL:[NSURL URLWithString:model.cover]];
        return item;
    } else {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
        return cell;
    }
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
